package com.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICorrectPapersService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * @author wufeng
 * @date 创建时间：2016年8月22日 上午9:27:36 
 */
@Controller
@RequestMapping("correctPapers")
public class CorrectPapersController {
	@Autowired
	private ICorrectPapersService correctPapersService;
	
	@RequestMapping("")
	public ModelAndView mv(){
		ModelAndView mv = new ModelAndView("examination/examList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("listData/{state}")
	public void listData(HttpServletRequest request, HttpServletResponse response,@PathVariable Integer state){
		String user = SessionUtil.getStringAttri(request,SessionData.nameKey_mng);
		DataVector<Map<String, Object>> listData = correctPapersService.getList(null, state, user, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("paperInfo/{id}/{examId}")
	public ModelAndView infoMv(@PathVariable Integer id, @PathVariable Integer examId){
		ModelAndView mv = new ModelAndView("examination/examInfo");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("id", id);
		mv.addObject("examId",examId);
		return mv;
		
	}
	@RequestMapping("correctPaper/{id}/{examId}")
	public ModelAndView correctPaperMv(@PathVariable Integer id, @PathVariable Integer examId){
		ModelAndView mv = new ModelAndView("examination/correctPaper");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("id", id);
		mv.addObject("examId",examId);
		return mv;
		
	}
	@RequestMapping("infoData/{id}")
	public void getInfodata(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer id){
		Map<String, Object> map = correctPapersService.getMap(id);
		String json = JSONSerielizer.toJson(map, null, null);
		OutputUtil.returnString(response, json);
	}
	@RequestMapping("confirm/{id}")
	public void confirm(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String id){
		String json = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = correctPapersService.confirm(id, map, user);
		OutputUtil.returnSuccess(response, isSuccess);
	}
}
